(function($) {
	$("#datu li.index_down").remove();
	//if ($.os.ios) {
		$("#datu").append("<li><a href='http://static.58.com/app/cx58.htm' onclick='clickLog(\"from=banner1\")' id='ggk'><img src='http://pic2.58.com/m58/m3/img/cx58180.png' width='320' height='90'></a></li><li><a href='http://app.toutiao.com/news_article/?utm_source=58' onclick='clickLog(\"from=banner2\")' id='fanqie'><img src='http://pic2.58.com/m58/m3/img/jrtt180.png' width='320' height='90'></a></li>");
		/*
	} else {
		$("#datu").append("<li><a href='http://dl.wandoujia.com/files/phoenix/latest/wandoujia-58tongcheng_hl.apk' onclick='clickLog(\"from=banner1\")' id='ggk'><img src='http://pic2.58.com/m58/m3/img/wdj0312.png' width='320' height='90'></a></li><li><a href='http://zp.58.com/yy/m/index?from=home_top_zpj' onclick='clickLog(\"from=banner2\")' id='fanqie'><img src='http://pic2.58.com/m58/m3/img/zp180310.png' width='320' height='90'></a></li>");	
	}
	*/
	scrollpic();
}) (jq);


function googleStatistics(para, referer) {
	if (typeof googleAnalytices === 'function') {
		googleAnalytices(para, referer);
	}
}

function scrollpic() {
	var slideX = (function() {
		var _slide = function(div_id, panel_id, duration) {
			var	_divId, _div, _ul, _panel, _eleX, _li_margin, _ul_width, _ul_margin,
			_startx = _index = 0, _duration = duration,autoScroll = true,
			_bindEvents = function() {
				_ul.bind("touchstart", _touchstart, false);
				_ul.bind("touchmove", _touchmove, false);
				_ul.bind("touchend", _touchend, false);
			},
			_click = function(e) {
				var img = e.target;
			},
			_move = function(e, sx, ex) {
				var max = _ul.children().length;
				if (ex > sx) {
					if (_index > 0) {
						_index --;
					}
				} else if (ex < sx){
					if (_index < max-1) {
						_index ++;
					}
				} else {
					_click(e);
					return;
				}
				var movex = (_index==0)?_ul_margin:-(_index * (_ul_width+_li_margin*2))+_ul_margin;
				_ul.css("-webkit-transition-duration", _duration);
				_ul.css("-webkit-transform", "translate3d("+movex+"px,0,0)");
				_panel.children("span").removeClass("curr").get(_index).className = "curr";
				
			};
			_touchstart = function(e) {
				autoScroll = false;
				e.stopPropagation();
				var finger0 = e.targetTouches[0];
				_startx = finger0.pageX;
				var transform = $(this).css("-webkit-transform");
				var translateX = parseInt(transform.split(/[(]|[,]|[)]/)[1]);
				_eleX = translateX || _ul_margin;
			},
			_touchmove = function(e) {
				e.preventDefault();
				e.stopPropagation();
				var finger0 = e.targetTouches[0];
				var moveX = finger0.pageX - _startx + _eleX;
				_ul.css({"-webkit-transition-duration": "0", "-webkit-transform": "translate3d("+moveX+"px, 0, 0)"});
			};
			_touchend = function(e) {
				var finger0 = e.changedTouches[0];
				_move(e, _startx, finger0.pageX);
				autoScroll = true;
				if (_index > 0) {
					googleStatistics( "/clicklocal/finger/banner1/", document.referer );
				} else {
					googleStatistics( "/clicklocal/finger/banner2/", document.referer );
				}
			};
			
			setInterval(function(){
				if (!autoScroll) {
					return;
				}
				if (_index > 0) {
					_index --;
					 $("#datu").css({"-webkit-transform" : "translate3d(0px, 0, 0)", "-webkit-transition": "300ms","transition":"300ms" });
					$(".panel_num").children("span").removeClass("curr").get(_index).className = "curr";
				} else {
					_index ++;
					 $("#datu").css({"-webkit-transform" : "translate3d(-320px, 0, 0)", "-webkit-transition": "300ms","transition":"300ms" });
					$(".panel_num").children("span").removeClass("curr").get(_index).className = "curr";
				}
				
			},4000);
			

			this.init = function() {
				_divId = div_id || "";
				if ("" != _divId) {
					_div = $("#"+_divId);
					_ul = _div.children("ul");
					var div_width = document.body.clientWidth;//parseInt(_div.css("width").replace("px", ""));
					var li1 = $(_ul.children("li").get(0));
					_li_margin = li1.css('margin-left') ? parseInt(li1.css("margin-left").replace("px", "")) : 0;
					_ul_width = parseInt(li1.css("width").replace("px", ""));
					_ul_margin = (div_width - _ul_width) / 2 - _li_margin;console.log(_ul_margin);
					_ul.css("-webkit-transform", "translate3d("+_ul_margin+"px, 0, 0)");
					_panel = (/^\./.test(panel_id)) ? $(panel_id) : $(("#"+panel_id).replace(/##/, "#"));
					var spans = '';
					for (var i=0, len=_ul.children("li").length; i<len; ++i) {
						spans += (0==i)?'<span class="curr"></span>':'<span></span>';
					}
					_panel.html(spans);
					_bindEvents();
				}
			};
		};
		_slide.bind = function(div_id, panel_id, duration) {
			var obj = new m58.slideX(div_id, panel_id, duration);
			obj.init();
		};
		return _slide;
	}) ();
	window.m58 = window.m58 || {};
	window.m58.slideX = slideX;	
	$(function() {
		//绑定
		m58.slideX.bind("pics", ".panel_num", "300ms");
	});
}

/*
$(".index_down").bind('click', function(e){
	if ($.os.ios) {
		//$(this).attr('href','http://itunes.apple.com/cn/app/id404612543?mt=8');
		window.location = 'http://itunes.apple.com/cn/app/id404612543?mt=8';
	}
	if ($.os.android) {
		//$(this).attr('href','http://download.58.com/productor/58client_v4.9.1.0_2.apk');
		window.location = 'http://download.58.com/productor/58client_v4.9.1.0_2.apk';
	}
	googleStatistics( "/clicklocal/banner1/" , document.referer );
	e.preventDefault();
	e.stopPropagation();
});
$("#fanqie").bind('click', function(e){
	return;
	if ($.os.ios) {
		//$(this).attr('href','http://www.fanqie.com/509channel?id=01');
		window.location = 'http://www.fanqie.com/509channel?id=01';
	}
	if ($.os.android) {
		//$(this).attr('href','http://www.fanqie.com/509channel?id=01');
		window.location = 'http://www.fanqie.com/509channel?id=01';
	}
	googleStatistics( "/clicklocal/banner2/" , document.referer );
	e.preventDefault();
	e.stopPropagation();
});

$("#ggk").bind('click', function(e){
	window.location = 'http://static.58.com/app/M_ggkyure.html?from=home_m';
	googleStatistics( "/clicklocal/banner1/" , document.referer );
	e.preventDefault();
	e.stopPropagation();
});
*/

/**
 *	如果cookie中含有mtg键值，则干掉banner
 */
$(function () {
	if ($.cookie.get('mtg')) {
		$('#pics').parent().hide();
	}
});
